package business_opportunity

import (
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/oppty/report"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/clue"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
	"strconv"
)

// 商机跟进控制器

type ReportFollowController struct {
	controller.BaseController
	reportFollowService clue.ReportFollowServiceIface
}

func NewReportFollowController(reportFollowService clue.ReportFollowServiceIface) *ReportFollowController {
	return &ReportFollowController{
		reportFollowService: reportFollowService,
	}
}

func (r *ReportFollowController) RegisterRouter(g *gin.RouterGroup) {
	g.POST("/save", r.SaveFollowRecord)
	g.GET("/list", r.GetFollowRecordList)
}

func (r *ReportFollowController) SaveFollowRecord(ctx *gin.Context) {
	req := report.SaveFollowRecordRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		stark.Logger.Errorf(ctx.Request.Context(), "SaveFollowRecord 参数错误:%s", err.Error())
		r.Fail(ctx, errcode.COMMON_ERROR, "参数不合法")
		return
	}

	if err := r.reportFollowService.SaveFollowRecord(ctx, &req); err != nil {
		stark.Logger.Errorf(ctx.Request.Context(), "SaveFollowRecord 保存失败:%s", err.Error())
		r.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	r.Success(ctx, map[string]interface{}{})
}

func (r *ReportFollowController) GetFollowRecordList(ctx *gin.Context) {
	reportId := ctx.Query("report_id")
	page := ctx.DefaultQuery("page", "1")
	pageSize := ctx.DefaultQuery("page_size", "10")

	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	list, total, err := r.reportFollowService.GetFollowRecordList(ctx, reportId, int32(p), int32(ps))
	if err != nil {
		r.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	data := map[string]interface{}{
		"total": total,
		"list":  list,
	}
	r.Success(ctx, data)
}
